import  subprocess
import IPy
ip=input("请输入你的IP：")
a=IPy.IP(ip)
for i in a:
        cmd = ("ping {} -0 -w 10".format(i))
        r=subprocess.Popen(cmd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
        r=r.stdout.read().decode('gbk')
        # r=subprocess.getoutput(cmd)
        # r=r.communicate().decode('gbk')
        try:
            assert "请求超时" not in r
            with open("ip_use.txt",'a+',encoding='utf-8') as w:
                if "TTL" in r:
                    w.write(f"{i}地址已被使用\n")
                else:
                    print(f"{i}地址未被使用！")
        except:
            print(f"{r}请求超时！")
